/**
 * <p>
 * Created on 2006-6-12 19:31:09 <br>
 * Copyright (c) 2006, All rights reserved.
 * </p>
 * @author Widen
 */
package com.widen.prima.editor.finance;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.widen.prima.Messages;
import com.widen.prima.finance.entites.AccountSubjectBo;
import com.widen.prima.util.Util;

public class SubjectBalanceCellModifier
    implements ICellModifier
{
    private Viewer viewer;
    private Shell shell = Display.getCurrent().getActiveShell();

    public SubjectBalanceCellModifier(Viewer viewer)
    {
        this.viewer = viewer;
    }

    /**
     * Returns whether the property can be modified
     * 
     * @param element the element
     * @param property the property
     * @return boolean
     */
    public boolean canModify(Object element, String property)
    {
        if (SubjectBalanceSettingPanel.SUBJECT_TYPE.equals(property))
        {
            return false;
        } else if (SubjectBalanceSettingPanel.SUBJECT_NAME.equals(property))
        {
            return false;
        } else if (SubjectBalanceSettingPanel.DIRECTION.equals(property))
        {
            return false;
        } else if (SubjectBalanceSettingPanel.BALANCE.equals(property))
        {
            boolean hasStartedKeepAccounts = Util.financeMgrService
                .hasStartedKeepAccounts();
            if (hasStartedKeepAccounts)
            {
                return false;
            } else
            {
                return true;
            }
        } else
        {
            return true;
        }
    }

    /**
     * Returns the value for the property
     * 
     * @param element the element
     * @param property the property
     * @return Object
     */
    public Object getValue(Object element, String property)
    {
        AccountSubjectBo subjectBo = (AccountSubjectBo) element;
        if (SubjectBalanceSettingPanel.SUBJECT_TYPE.equals(property))
            return subjectBo.getSubjectType();
        else if (SubjectBalanceSettingPanel.SUBJECT_NAME.equals(property))
            return subjectBo.getFullName();
        else if (SubjectBalanceSettingPanel.DIRECTION.equals(property))
            return subjectBo.getDirection();
        else if (SubjectBalanceSettingPanel.BALANCE.equals(property))
            return Util.double_formatter.format(subjectBo.getCurrentMoney());
        else
            return null;
    }

    /**
     * Modifies the element
     * 
     * @param element the element
     * @param property the property
     * @param value the value
     */
    public void modify(Object element, String property, Object value)
    {
        if (element instanceof Item)
            element = ((Item) element).getData();

        AccountSubjectBo subjectBo = (AccountSubjectBo) element;
        if (SubjectBalanceSettingPanel.BALANCE.equals(property))
        {
            String stringValue = (String) value;
            if (stringValue != null && stringValue.length() > 0)
            {
                if (!Util.validDouble(stringValue))
                {
                    MessageBox messageBox = new MessageBox(
                        shell,
                        SWT.ICON_INFORMATION | SWT.OK);
                    messageBox.setText(Messages
                        .getString("SubjectBalanceCellModifier.prompt.info")); //$NON-NLS-1$
                    messageBox
                        .setMessage(Messages
                            .getString("SubjectBalanceCellModifier.input.not.correct")); //$NON-NLS-1$
                    messageBox.open();
                    return;
                }
                subjectBo.setCurrentMoney(Util.toDouble(stringValue));
            }
        }

        // Force the viewer to refresh
        viewer.refresh();
    }
}